﻿namespace PowerSong.Import
    {
    using PowerSong.SongDatabase;
    using System.Collections.Generic;
    using PowerSong.UserInterface;

    public class WebsiteImport : BaseImport
        {

        private bool downloadSongs = false;

        private bool downloadStyles = false;

        private bool downloadBbe = false;

        public WebsiteImport(Database destinationDatabase, bool downloadSongs, bool downloadStyles, bool downloadBbe)
            : base(destinationDatabase)
            {
            this.downloadSongs = downloadSongs;
            this.downloadStyles = downloadStyles;
            this.downloadBbe = downloadBbe;
            }

        protected override void Import(Category category, List<string> messages)
            {

            // The logic for downloading songs and styles
            if (downloadSongs || downloadStyles)
                {

                // Go ahead and actually download the data
                SetProgress(0, "Downloading package...");
                var url = string.Format(Infrastructure.DatabasesUrl, "Complete.2.0.0.PowerSong");                
                var data = Infrastructure.DownloadFile(url);
                var database = Database.Load(data);

                // Import the data into the database
                if (downloadSongs)
                    {
                    SetProgress(0, "Importing songs...");
                    foreach (var song in database.Songs.GetAllSongs())
                        {
                        song.Categories.Clear();
                        song.Categories.Add(category);
                        Destination.Songs.Write(song);
                        }
                    }
                if (downloadStyles)
                    {
                    SetProgress(0, "Importing styles...");
                    foreach (var style in database.Styles.GetAllStyles())
                        {
                        Destination.Styles.Write(style);
                        }
                    }

                }

            // The logic for downloading the Bible translations
            if (downloadBbe)
                {
                SetProgress(0, "Importing Bible translation...");
                var url = string.Format(Infrastructure.DatabasesUrl, "Bible.BBE.0.9.6.Bible");
                var data = Infrastructure.DownloadFile(url);
                BibleTranslationManager.InstallTranslation(data, "Bible in Basic English");
                }

            }

        }

    }
